package game.mapviewer 
{
	import com.greensock.TweenLite;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import game.map.Army;
	import game.map.Unit;
	import game.map.UnitTypes;
	import game.mapviewer.popups.ArmyOrdersPopup;
	import game.unitmanager.ArmyController;
	import game.unitmanager.OrderTypes;
	/**
	 * ...
	 * @author rnk
	 */
	public class ArmyIcon extends Sprite
	{
		
		public var papa:MapViewer;
		private var spriteHolder:Sprite;
		private var orderIcon:MovieClip;
		public var model:Army;
		public var controller:ArmyController;
		
		public function ArmyIcon(army:Army,controller:ArmyController) 
		{
			spriteHolder = new Sprite();
			addChild(spriteHolder);
			this.controller = controller;
			model = army;
			Refresh();
			
			
			SetListeners(true);
		}
		
		private function SetListeners(orly:Boolean):void 
		{
			(orly?addEventListener:removeEventListener)(MouseEvent.MOUSE_OVER, OnMouseOver);
			(orly?addEventListener:removeEventListener)(MouseEvent.CLICK, OnMouseClick);
			(orly?addEventListener:removeEventListener)(MouseEvent.MOUSE_OUT, OnMouseOut);
			
			useHandCursor = buttonMode = orly;
		}
		
		private function OnMouseClick(e:MouseEvent):void 
		{
			papa.popupsLayer.ShowPopup(ArmyOrdersPopup, parent.x, parent.y, { callback:OnOrdersPopupCallback }, false);
		}
		
		private function OnOrdersPopupCallback(orderNo:int):void 
		{
			controller.SetOrder(orderNo);
		}
		
		private function OnMouseOut(e:MouseEvent):void 
		{
			this.filters = [];
			
		}
		
		private function OnMouseOver(e:MouseEvent):void 
		{
			this.filters = [new GlowFilter(0xFFFF00,1,10,10)];
		}
		
		public function ClearOrder():void
		{
			if (orderIcon)
			{
				removeChild(orderIcon);
				orderIcon = null;
			}
		}
		
		public function ShowOrder(orderNo:int):void
		{
			ClearOrder();
			
			if (orderNo < 0) return;
			
			switch (orderNo) 
			{
				case OrderTypes.MARCH:
					orderIcon = new march_icon();
					orderIcon.filters = [new GlowFilter(0x27E817,1,10,10,3)];
				break;
				case OrderTypes.DEFEND:
					orderIcon = new defend_icon();
					orderIcon.filters = [new GlowFilter(0x00A6FF,1,10,10,3)];
				break;
				case OrderTypes.SUPPORT:
					orderIcon = new support_icon();
					orderIcon.filters = [new GlowFilter(0xFF00FF,1,10,10,3)];
				break;
				case OrderTypes.RAID:
					orderIcon = new raid_icon();
					orderIcon.filters = [new GlowFilter(0xFF7B2D,1,10,10,3)];
				break;
			}
			
			orderIcon.x = -orderIcon.width / 2;
			orderIcon.y = -orderIcon.height / 2;
			
			addChild(orderIcon);
		}
		
		public function Refresh():void
		{
			while (spriteHolder.numChildren) spriteHolder.removeChildAt(0);
			
			//loyalty banner
			var banners:Array = [banner1, banner2, banner3];
			if (controller.loyalty > 0 && controller.loyalty <= banners.length)
			{
				var bannerClass:Class = banners[controller.loyalty - 1] as Class;
				var banner:MovieClip = new bannerClass();
				spriteHolder.addChild(banner);
			}
				

			
			if (model.units.length > 0)
			{
				var unitTypes:Object = {};
				for (i = 0; i < model.units.length; i++)
				{
					var unit:Unit = model.units[i];
					var iconClass:Class;
					
					switch (unit.type) 
					{
						case UnitTypes.FOOTMAN:
							iconClass = footman;
						break;
						case UnitTypes.KNIGHT:
							iconClass = knight;
						break;
						case UnitTypes.ARCHER:
							iconClass = archer2;
						break;
						case UnitTypes.LANDSKNEHT:
							iconClass = landsknecht;
						break;
					}
					
					var unitIcon:MovieClip = new iconClass();
					if (!unitTypes[unit.type]) unitTypes[unit.type] = [];
					unitTypes[unit.type].push(unitIcon);
				}
				
				var rowOfUnits:Array = [];
				var maximumColumnLength:int = 0;
				var magicDx:Number = 10;
				var magicDy:Number = 8;
				var dx:Number = 0;
				
				for each (var units:Array in unitTypes) 
				{
					
					maximumColumnLength = Math.max(maximumColumnLength, units.length);
					var columnOfUnits:Array = [];
					var bounds:Rectangle;
					for (var i:int = 0; i < units.length; i++)
					{
						
						unitIcon = units[i];
						
						bounds = unitIcon.getBounds(unitIcon);
						
						unitIcon.x = dx - bounds.x;
						
						
						if (i > 0)
						{
							unitIcon.x += magicDx*i;
							unitIcon.y += magicDy*i;
						}
						
						columnOfUnits.push(unitIcon);
					}
					rowOfUnits.push(columnOfUnits);
					dx += bounds.width;
				}
				
				rowOfUnits.sortOn(['length']);
				
				/*rowOfUnits.sort(
					function (a:Array, b:Array):Number
					{
						var aPrice:Number = a.length;
						var bPrice:Number = b.length;
						
						if (aPrice > bPrice) 
						{
							return 1;
						} 
						else 
						if (aPrice < bPrice) 
						{
							return -1;
						} 
						else  
						{
							//aPrice == bPrice
							return 0;
						}
						
					}
				);*/
				
				for (var j:int = 0; j < maximumColumnLength; j++)
				{
					for (var k:int = 0; k < rowOfUnits.length; k++)
					{
						if (rowOfUnits[k].length>j)
						spriteHolder.addChild(rowOfUnits[k][j]);
					}
				}
				
				spriteHolder.x = -dx / 2;
				
				if (banner)
				banner.x = -spriteHolder.x-15;
			}
		}
		
		public function MoveArmy(provinceUid:String,onFinishCallback:Function):void
		{
			//detach from province holder
			papa.spriteHolder.addChild(this);
			this.x = papa.GetProvinceHolder(model.province.uid).x;
			this.y = papa.GetProvinceHolder(model.province.uid).y;
			
			//get coordinates of destination province holder
			var dest:Point = new Point(papa.GetProvinceHolder(provinceUid).x, papa.GetProvinceHolder(provinceUid).y);
			
			//create move tween
			TweenLite.to(this, 1.0, { x:dest.x, y:dest.y , onComplete:OnMoveFinish, onCompleteParams:[provinceUid,onFinishCallback] } );
			
		}
		
		private function OnMoveFinish(provinceUid:String,callback:Function):void
		{
			//attach to destination province holder
			papa.GetProvinceHolder(provinceUid).addChild(this);
			this.x = 0;
			this.y = 0;
			
			//call callback
			if (Boolean(callback)) callback();
		}
	}

}